package com.example.testsignalr

import android.os.AsyncTask
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import com.microsoft.signalr.HubConnection

import java.util.*


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        val textView = findViewById<TextView>(R.id.tvMain)
        val listView = findViewById<ListView>(R.id.lvMessages)
        val sendButton = findViewById<Button>(R.id.bSend)
        val joinButton = findViewById<Button>(R.id.bJoin)
        val editText = findViewById<EditText>(R.id.etMessageText)
        val messageList: List<String> = ArrayList()
        val arrayAdapter = ArrayAdapter(
            this@MainActivity,
            android.R.layout.simple_list_item_1, messageList
        )
        listView.adapter = arrayAdapter

        val client = RxSignalrClient("http://192.168.1.7:50098/chat")
        client.subscribeOn<String>("broadcast").subscribe {
            runOnUiThread {
                arrayAdapter.add(it)
                arrayAdapter.notifyDataSetChanged()
            }
        }
        client.subscribeOn<Int>("refresh").subscribe {
            joinButton.text = it.toString()
        }


//        val hubConnection = HubConnectionBuilder.create("http://192.168.1.7:54227/chat").build()
//        hubConnection.on("broadcast", { message: String? ->
//            runOnUiThread {
//                arrayAdapter.add(message)
//                arrayAdapter.notifyDataSetChanged()
//            }
//        }, String::class.java)
        joinButton.setOnClickListener {
            client.send("Join","linx-android1")
        }
        sendButton.setOnClickListener {
            val message = editText.text.toString()
            editText.setText("")
            try {
                if (!client.isConnected()) {
                    Toast.makeText(this, "disconnected", Toast.LENGTH_SHORT).show()
                }
                client.send("Send", "linx-android1", message)
//                client.hubConnection.send("Send", "linx-android", message)
//                println(client.hubConnection)
//                val conn = client.hubConnection
//                conn.send("Send", "linx-android2", message)
//                if (hubConnection.connectionState!=HubConnectionState.CONNECTED) {
//                    Toast.makeText(this,"disconnected",Toast.LENGTH_SHORT).show()
//                }
//                hubConnection.send("Send", "linx-android",message)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        client.startConnection()
        Toast.makeText(this, "iijdie", Toast.LENGTH_SHORT).show()
//        HubConnectionTask().execute(hubConnection)
    }

    internal class HubConnectionTask : AsyncTask<HubConnection?, Void?, Void?>() {
        override fun onPreExecute() {
            super.onPreExecute()
        }

        override fun doInBackground(vararg params: HubConnection?): Void? {
            val hubConnection = params[0]
            hubConnection!!.start().blockingAwait()
            return null
        }
    }

    fun joinClick(view: View) {

    }


}